local sk__qianhuan = fk.CreateSkill {

  name = "sk__qianhuan",

  tags = { Skill.Compulsory, },

}
Fk:addQmlMark {
  name = "SkSkills_jilv",
  qml_path = "packages/water_jilve/qml/SkSkills",
  how_to_show = function(name, value, p)
    if string.startsWith(name, "@[SkSkills_jilv]") and type(value) == "table" then
      return tostring(#value)
    end
    return ""
  end,
}



sk__qianhuan:addEffect(fk.GameStart, {
  name = "sk__qianhuan",
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__qianhuan.name) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = player:getMark("sk__qianhuan_deputy") == 0 and 2 or 4
    if player.deputyGeneral ~= "" and player.deputyGeneral ~= "sk__zuoci" then
      room:setPlayerMark(player, "sk__qianhuan_deputy", 1)
      n = 4
      room:returnToGeneralPile({ player.deputyGeneral })
      room:changeHero(player, "", false, true, true, false, false)
    end
    if player.dead then return end
    local mark = player:getTableMark("@[SkSkills_jilv]sk__qianhuan")
    mark = table.filter(mark, function(s)
      return player:hasSkill(s, true)
    end)
    if #mark > 0 then
      local lose = table.simpleClone(mark)
      local result = room:askForCustomDialog(player, sk__qianhuan.name,
        "packages/utility/qml/ChooseSkillBox.qml", {
          mark, 0, 2, "#sk__qianhuan-reserve"
        })
      if result ~= "" and #result > 0 then
        mark = table.simpleClone(result)
        for _, s in ipairs(mark) do
          table.removeOne(lose, s)
        end
        room:setPlayerMark(player, sk__qianhuan.name, mark)
      end
      if #lose > 0 then
        room:handleAddLoseSkills(player, "-" .. table.concat(lose, "|-"), nil, true, false)
      end
    end
    local generals = room:getNGenerals(3)
    if #generals == 0 then return end
    local skills = {}
    for i = 1, #generals, 1 do
      table.insert(skills, Fk.generals[generals[i]]:getSkillNameList(true))
    end
    local result = room:askForCustomDialog(player, sk__qianhuan.name,
      "packages/utility/qml/ChooseGeneralSkillsBox.qml", {
        generals, skills, 0, n, "#sk__qianhuan-choice:::" .. n, true
      })
    room:returnToGeneralPile(generals)
    local choices = {}
    if result ~= "" then
      choices = result
    end
    if #choices > 0 then
      table.insertTableIfNeed(mark, choices)
      room:setPlayerMark(player, "@[SkSkills_jilv]sk__qianhuan", mark)
      room:handleAddLoseSkills(player, table.concat(choices, "|"), nil, true, false)
    end
  end,
})
sk__qianhuan:addEffect(fk.TurnStart, {
  name = "sk__qianhuan",
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__qianhuan.name) then
      return target == player
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = player:getMark("sk__qianhuan_deputy") == 0 and 2 or 4
    if player.deputyGeneral ~= "" and player.deputyGeneral ~= "sk__zuoci" then
      room:setPlayerMark(player, "sk__qianhuan_deputy", 1)
      n = 4
      room:returnToGeneralPile({ player.deputyGeneral })
      room:changeHero(player, "", false, true, true, false, false)
    end
    if player.dead then return end
    local mark = player:getTableMark("@[SkSkills_jilv]sk__qianhuan")
    mark = table.filter(mark, function(s)
      return player:hasSkill(s, true)
    end)
    if #mark > 0 then
      local lose = table.simpleClone(mark)
      local result = room:askForCustomDialog(player, sk__qianhuan.name,
        "packages/utility/qml/ChooseSkillBox.qml", {
          mark, 0, 2, "#sk__qianhuan-reserve"
        })
      if result ~= "" and #result > 0 then
        mark = table.simpleClone(result)
        for _, s in ipairs(mark) do
          table.removeOne(lose, s)
        end
        room:setPlayerMark(player, sk__qianhuan.name, mark)
      end
      if #lose > 0 then
        room:handleAddLoseSkills(player, "-" .. table.concat(lose, "|-"), nil, true, false)
      end
    end
    local generals = room:getNGenerals(3)
    if #generals == 0 then return end
    local skills = {}
    for i = 1, #generals, 1 do
      table.insert(skills, Fk.generals[generals[i]]:getSkillNameList(true))
    end
    local result = room:askForCustomDialog(player, sk__qianhuan.name,
      "packages/utility/qml/ChooseGeneralSkillsBox.qml", {
        generals, skills, 0, n, "#sk__qianhuan-choice:::" .. n, true
      })
    room:returnToGeneralPile(generals)
    local choices = {}
    if result ~= "" then
      choices = result
    end
    if #choices > 0 then
      table.insertTableIfNeed(mark, choices)
      room:setPlayerMark(player, "@[SkSkills_jilv]sk__qianhuan", mark)
      room:handleAddLoseSkills(player, table.concat(choices, "|"), nil, true, false)
    end
  end,
})
sk__qianhuan:addEffect(fk.EventLoseSkill, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data == self
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@[SkSkills_jilv]sk__qianhuan", 0)
  end,
})

return sk__qianhuan
